<!--
 * @FilePath     : /ollama-translation-extension/components/Setting.vue
 * @Description  :  popup设置页面组件
 * @Author       : xinbaiyu
 * @Date         : 2025-03-18 15:39:31
 * @Version      : 0.0.1
 * Copyright    : G AUTOMOBILE RESEARCH INSTITUTE CO.,LTD Copyright (c) 2025.
-->
<template>
  <el-form
    ref="ruleFormRef"
    style="max-width: 600px"
    :model="ruleForm"
    :rules="rules"
    label-width="auto"
    class="demo-ruleForm"
    :size="formSize"
    status-icon
  >
    <el-form-item label="ollama地址" prop="apiUrl">
      <el-input v-model="ruleForm.apiUrl" />
    </el-form-item>
    <el-form-item label="模型名" prop="modelName">
      <el-input v-model="ruleForm.modelName" />
    </el-form-item>
    <el-form-item label="语言" prop="language">
      <el-select v-model="ruleForm.language" placeholder="请选择">
        <el-option
          v-for="item in selectOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
    </el-form-item>
    <el-form-item class="form-item-btn">
      <el-button type="primary" @click="submitForm(ruleFormRef)">
        保存
      </el-button>
      <el-button @click="resetForm(ruleFormRef)">重置</el-button>
    </el-form-item>
  </el-form>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import {
  ElMessage,
  type ComponentSize,
  type FormInstance,
  type FormRules,
} from 'element-plus'
import { storage } from 'wxt/storage'

const formSize = ref<ComponentSize>('default')
const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive<RuleForm>({
  apiUrl: '',
  modelName: '',
  language: '',
})
const selectOptions = [
  {
    value: 'Chinese',
    label: '中文',
  },
  {
    value: 'English',
    label: 'English',
  },
]

/**
 * @Author       : xinbaiyu
 * @Date         : 2025-03-21 22:08:14
 * @Version      : 0.0.1
 * @Description  : 判断输入的url是否符合规范
 * @param         {*} _:
 * @param         {*} url:
 * @param         {*} callback:
 * @return        {*}
 */
function validateHttpUrl(_: any, url: string, callback: any) {
  if (!url.endsWith('/')) {
    callback()
  } else {
    return callback(new Error('输入地址请勿以/结尾'))
  }
}

const rules = reactive<FormRules<RuleForm>>({
  apiUrl: [
    { required: true, message: '请输入ollama地址', trigger: 'blur' },
    { validator: validateHttpUrl, trigger: 'blur' },
  ],
  modelName: [{ required: true, message: '请输入模型名称', trigger: 'blur' }],
  language: [{ required: true, message: '请选择翻译语言', trigger: 'blur' }],
})

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      await storage.setItem('local:settings', ruleForm)
      ElMessage.success('保存成功')
    } else {
      console.log('error submit!', fields)
      ElMessage.error('参数错误')
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}

onMounted(async () => {
  const settings = await storage.getItem<RuleForm>('local:settings')
  if (settings) {
    Object.assign(ruleForm, settings)
  }
})
</script>
<style scoped>
.form-item-btn {
  margin: 20px 0 0 !important;
}
.form-item-btn :deep() .el-form-item__content {
  justify-content: center;
}
</style>
